//
//  MDCycleScrollView.swift
//  麦兜直播
//
//  Created by Apple on 16/10/19.
//  Copyright © 2016年 yeqingyu. All rights reserved.
//

import UIKit
private let timerDur:CGFloat = 3
private let cycleScrollId = "MDCycleScrollCellID"
class MDCycleScrollView: UIView {
    var timer:Timer?
    
    var cycleGroup:[MDCycleScroll]?{
        didSet{
        pageControll.numberOfPages = (cycleGroup?.count)!
        collectionView.reloadData()
        addTimer()
        }
    }
    
    @IBOutlet weak var collectionView: UICollectionView!
    @IBOutlet weak var pageControll: UIPageControl!
    @IBOutlet weak var layout: UICollectionViewFlowLayout!

    
    override func awakeFromNib() {
        super.awakeFromNib()
        // 设置该控件不随着父控件的拉伸而拉伸
        autoresizingMask = UIViewAutoresizing()
        collectionView.register(UINib(nibName: "MDCycleScrollCell", bundle: nil), forCellWithReuseIdentifier: cycleScrollId)
        
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        layout.itemSize = bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
    }
}

extension MDCycleScrollView{
    class func show()->MDCycleScrollView{
       return Bundle.main.loadNibNamed("MDCycleScrollView", owner: nil, options: nil)?.last as! MDCycleScrollView
    }
    
     //添加定时器
    fileprivate  func addTimer() {
    timer =  Timer(timeInterval: TimeInterval(timerDur), target: self, selector: #selector(self.scrollToNextPage), userInfo: nil, repeats: true)
     RunLoop.main.add(timer!, forMode: RunLoopMode.defaultRunLoopMode)
    }
     //移除定时器
    fileprivate func removeTimer(){
        timer?.invalidate()//从运行循环移除
        timer = nil
    }
   
    @objc fileprivate func scrollToNextPage(){
        let contentOffSet = collectionView.contentOffset.x
        let totle = collectionView.bounds.size.width * CGFloat(((cycleGroup?.count)!-1))
        var offSet:CGFloat = contentOffSet+collectionView.bounds.size.width
        var animation = true
        if offSet > totle {
            offSet = 0
            animation = false
        }
        collectionView.setContentOffset(CGPoint(x: offSet, y: 0), animated: animation)
    }
}
// MARK:- 数据源方法
extension MDCycleScrollView:UICollectionViewDataSource{

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return cycleGroup?.count ?? 0
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cycleScrollId, for: indexPath) as! MDCycleScrollCell
        cell.cycleScroll = cycleGroup?[indexPath.row]
        return cell
    }
}

// MARK:- 代理方法
extension MDCycleScrollView:UIScrollViewDelegate{
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        let index = Int((scrollView.contentOffset.x+screenWidth*0.5)/screenWidth)
        pageControll.currentPage = index
        
    }
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        removeTimer()
    }
    
    func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool) {
        addTimer()
    }
}

